package com.five.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.admin.dto.EditStatusDto;
import com.five.admin.dto.ProductEditDto;
import com.five.admin.dto.ProductQueryDto;
import com.five.admin.server.ProductImageService;
import com.five.admin.server.ProductService;
import com.five.admin.vo.ProductAdminVo;
import com.five.admin.vo.ProductDetailsVo;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.pojo.dto.ProductAddDto;
import com.five.pojo.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Resource
    private ProductService productService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result<?> list(@RequestBody ProductQueryDto queryDto){
        System.err.println("查询条件是-----"+queryDto);
        Page<ProductAdminVo> resultPage = productService.findList(queryDto);
        return Result.buildSuccess(resultPage.getTotal(), resultPage.getRecords());
    }

    //添加
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Valid ProductAddDto productAddDto){
        boolean save = productService.save(productAddDto);
        return save ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPERATE_FAIL);
    }

    /**
     * 查询一个，用来回显
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryOne/{id}", method = RequestMethod.GET)
    public Result queryById(@PathVariable("id") Long id){
        ProductDetailsVo productDetailsVo = productService.getById(id);
        return productDetailsVo != null ? Result.buildSuccess(productDetailsVo) : Result.buildFail(ResultEnum.UNKNOWN_EXCEPTION);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Long id){
        log.info("删除商品id：{}", id);
        boolean remove = productService.deleteProductById(id);
        return remove ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPERATE_FAIL);
    }

    /**
     * 修改
     * @param productEditDto
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody ProductEditDto productEditDto){
        System.err.println(productEditDto);
        boolean updated = productService.updateProductById(productEditDto);
        return updated ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPERATE_FAIL);
    }


    @RequestMapping(value = "/{id}/status", method = RequestMethod.PUT)
    public Result editUpdate(@PathVariable("id") Long id, @RequestBody EditStatusDto editStatusDto){
        log.info("修改{}商品状态：{}",id, editStatusDto.getProductStatus());
        Product product = Product.builder().id(id).productStatus(editStatusDto.getProductStatus()).build();
        boolean updated = productService.updateById(product);
        return updated? Result.buildSuccess() : Result.buildFail(ResultEnum.OPERATE_FAIL);
    }
}
